package com.max.common.oauth.oauth20;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.max.common.util.XMLConfig;

public class Config 
{

	private static final String OAUTH20_FILE_NAME = "oauth20.xml";
	
	private static Map<String,Authorization> authorizationMap = null;
	
	private static Map<String,Client> clientMap = null;
	
	private static Config instance = null;
	
	static
	{
		getInstance();
	}
	
	private Config()
	{
	}
	
	private static synchronized Config getInstance()
	{
		if (instance == null)
		{
			instance = new Config();
			
			try 
			{
				URL url = Config.class.getClassLoader().getResource(OAUTH20_FILE_NAME);
				XMLConfig config = XMLConfig.getInstance(new File(url.getFile()));
		        
				/**prase authorization*/
		        Map<String,Map<String,String>> pmap = config.getAllValues("oauth/authorization", "name");
		        for (Iterator<Entry<String, Map<String, String>>> iter = pmap.entrySet().iterator(); iter.hasNext();)
		        {
		        	 Entry<String,Map<String,String>> entry = iter.next();
		        	 String name = entry.getValue().get("name");
		        	 String method = entry.getValue().get("method");
		        	 String authorizeURL = entry.getValue().get("authorizeURL");
		        	 String accessTokenURL = entry.getValue().get("accessTokenURL");
		        	 
		        	 Authorization authorization = new Authorization(
		        			 method, authorizeURL, accessTokenURL);
		        	 
		        	 if(authorizationMap == null)
		        		 authorizationMap = new HashMap<String,Authorization>(4);
		        	 authorizationMap.put(name, authorization);
		        }
		        
		        
		        /**prase client*/
		        Map<String,Map<String,String>> cmap = config.getAllValues("oauth/client", "name");
		        for (Iterator<Entry<String, Map<String, String>>> iter = cmap.entrySet().iterator(); iter.hasNext();)
		        {
		        	 Entry<String,Map<String,String>> entry = iter.next();
		        	 String name = entry.getValue().get("name");
		        	 String client_id = entry.getValue().get("client_id");
		        	 String client_secret = entry.getValue().get("client_secret");
		        	 String grant_type = entry.getValue().get("grant_type");
		        	 String redirect_uri = entry.getValue().get("redirect_uri");
		        	 String authorization = entry.getValue().get("authorization");
		        	 
		        	 Client client = new Client(client_id, client_secret,grant_type,redirect_uri, 
		        			 authorizationMap==null?null:authorizationMap.get(authorization));
		        	 
		        	 if(clientMap == null)
		        		 clientMap = new HashMap<String,Client>(4);
		        	 clientMap.put(name, client);
		        }
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		
		return instance;
	}

	public static Map<String, Authorization> getAuthorizationMap() 
	{
		return authorizationMap;
	}

	public static Map<String, Client> getClientMap() 
	{
		return clientMap;
	}
}


class Authorization
{
	private String method = null;
	
	private String authorizeURL = null;
	
	private String accessTokenURL = null;

	public Authorization(String method, String authorizeURL,
			String accessTokenURL) 
	{
		this.method = method;
		this.authorizeURL = authorizeURL;
		this.accessTokenURL = accessTokenURL;
	}

	public String getMethod() 
	{
		return method;
	}

	public void setMethod(String method) 
	{
		this.method = method;
	}

	public String getAuthorizeURL() 
	{
		return authorizeURL;
	}

	public void setAuthorizeURL(String authorizeURL) 
	{
		this.authorizeURL = authorizeURL;
	}

	public String getAccessTokenURL() 
	{
		return accessTokenURL;
	}

	public void setAccessTokenURL(String accessTokenURL) 
	{
		this.accessTokenURL = accessTokenURL;
	}
}

class Client
{
	private String client_id = null;
	
	private String client_secret = null;
	
	private String grant_type = null;

	private String redirect_uri = null;
	
	private Authorization authorization = null;

	public Client(String clientId, String clientSecret, String grantType,
			String redirectUri, Authorization authorization) 
	{
		this.client_id = clientId;
		this.client_secret = clientSecret;
		this.grant_type = grantType;
		this.redirect_uri = redirectUri;
		this.authorization = authorization;
	}

	public String getClient_id() 
	{
		return client_id;
	}

	public void setClient_id(String clientId) 
	{
		client_id = clientId;
	}

	public String getClient_secret() 
	{
		return client_secret;
	}

	public void setClient_secret(String clientSecret) 
	{
		client_secret = clientSecret;
	}

	public String getGrant_type() 
	{
		return grant_type;
	}

	public void setGrant_type(String grantType) 
	{
		grant_type = grantType;
	}

	public String getRedirect_uri() 
	{
		return redirect_uri;
	}

	public void setRedirect_uri(String redirectUri) 
	{
		redirect_uri = redirectUri;
	}
	
	public Authorization getAuthorization() 
	{
		return authorization;
	}

	public void setAuthorization(Authorization authorization) 
	{
		this.authorization = authorization;
	}
}
